home *** CD-ROM | disk | FTP | other *** search
- Path: rcp6.elan.af.mil!rscernix!danpop
- From: danpop@mail.cern.ch (Dan Pop)
- Newsgroups: comp.lang.c
- Subject: Re: read/write integers to files
- Date: 7 Mar 96 12:42:46 GMT
- Organization: CERN European Lab for Particle Physics
- Message-ID: <danpop.826202566@rscernix>
- References: <313EBF65.4E82@www.inia.net.au>
- NNTP-Posting-Host: ues5.cern.ch
- X-Newsreader: NN version 6.5.0 #7 (NOV)
-
- In <313EBF65.4E82@www.inia.net.au> Jason Collins <jason@www.inia.net.au> writes:
-
- >#include <stdio.h>
- >#include <stdlib.h>
- >
- >FILE *filePtr;
-
- What's the reason for making filePtr global???
-
- >void main()
- ^^^^
- Read the FAQ before posting!
-
- >{
- > char ctr;
- >
- > filePtr=fopen("count.dat", "ab");
-
- Open your C book and see what "ab" means. It's definitely not what you
- want in this context!
-
- BTW, how do you know the fopen call has succeeded?
-
- > fscanf(filePtr, "%d", &ctr);
-
- %d expects a pointer to int and &ctr is a pointer to char.
-
- > fclose(filePtr);
- >
- > ctr++;
- >
- > filePtr=fopen("count.dat", "wb");
-
- Again, "wb" is not what you want here. And you have no clue whether
- fopen succeeded or not!
-
- > fprintf(filePtr, "%d", ctr);
-
- Ending every line with a newline character is always a good idea.
-
- > fclose(filePtr);
- >}
-
- If you want to read/write the integer in binary format (and not in text
- format, as you did in this program), check the functions fread/fwrite
- in your C book.
-
- Dan
- --
- Dan Pop
- CERN, CN Division
- Email: danpop@mail.cern.ch
- Mail: CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
-